package forum.dao;

import forum.model.Reply;
import forum.util.DBUtil;
import lombok.SneakyThrows;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @author haomin
 * @date 2022/06/30 16:38
 **/
public class ReplyDao {

    // 查询主贴回复数
    @SneakyThrows
    public int tReplyCount(int tid){
        try (Connection c = DBUtil.connection()){
            String sql = "select count(*) from reply where tID = ?;";
            try (PreparedStatement ps = c.prepareStatement(sql)){
                ps.setInt(1,tid);
                try (ResultSet rs = ps.executeQuery()){
                    rs.next();
                    int count = rs.getInt("count(*)");
                    updateTReplyCount(tid,count);
                    return count;
                }
            }
        }
    }

    // 更新主贴回复数
    @SneakyThrows
    public void updateTReplyCount(int tID, int count) {
        String sql = "update topic set tReplyCount = ? where tID = ?";
        try (Connection c = DBUtil.connection()) {
            try (PreparedStatement ps = c.prepareStatement(sql)) {
                ps.setInt(1,count);
                ps.setInt(2,tID);

                ps.executeUpdate();
            }
        }
    }

    // 根据tid查询所有回复
    @SneakyThrows
    public List<Reply> selectListByTid(int tid){
        List<Reply> list = new ArrayList<>();
        String sql = "select rID, uID, rTopic, rContent, rTime, rClickCount from reply where tid = ? order by rTime desc";
        try (Connection c = DBUtil.connection()){
            try (PreparedStatement ps = c.prepareStatement(sql)){
                ps.setInt(1,tid);
                System.out.println("DEBUG: " + ps);

                try (ResultSet rs = ps.executeQuery()){
                    while (rs.next()){
                        Reply reply = new Reply();
                        reply.rID = rs.getInt("rID");
                        reply.tID = tid;
                        reply.uID = rs.getInt("uID");
                        reply.username = new UserDao().selectUserNameByUid(reply.uID);
                        reply.rTopic = rs.getString("rTopic");
                        reply.rContent = rs.getString("rContent");
                        String Time = rs.getString("rTime");
                        reply.rTime = Time.substring(0,Time.length()-5);
                        reply.rClickCount = rs.getInt("rClickCount");

                        list.add(reply);
                    }
                }

            }
        }
        return list;
    }

    // 插入主贴
    @SneakyThrows
    public void insert(int tid, Integer uID, String content) {
        try (Connection c = DBUtil.connection()){
            String sql = "insert into reply (tID,uID,rContent) values (?,?,?)";
            try (PreparedStatement ps = c.prepareStatement(sql)){
                ps.setInt(1,tid);
                ps.setInt(2,uID);
                ps.setString(3,content);

                ps.executeUpdate();
                System.out.println("插入完成");
            }
        }
    }
}